<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/admin/adminPage/AdminIndex' }">
      <i class="el-icon-s-promotion"></i> 后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item>添加主题住宿景色图片</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>

  <el-form style="padding-left: 150px" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="地点" prop="location">
      <el-input style="width: 250px" v-model="ruleForm.location"></el-input>
    </el-form-item>
    <el-form-item label="主题住宿" prop="themeName">
      <el-select style="width: 250px" v-model="ruleForm.themeId" placeholder="请选择">
        <el-option
            v-for="item in themeOptions"
            :key="item.id"
            :label="item.themeName"
            :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="地点图片">
      <!--上传组件开始-->
      <el-upload
          :action="'http://localhost:9580/upload?dir='+this.dir"
          name="picFile"
          :limit="1"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      <!--上传组件结束-->
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
export default {
  data(){
    return{
      dir:'主题住宿',
      themeOptions:[],
      ruleForm:{
        location:'',
        themeId:'',
        url:''
      },
      rules:{
        location: [
          {required: true, message: '请输入地点', trigger: 'blur'}
        ]
      },
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods:{
    submitForm(formName) {
          let url = 'http://localhost:9580/themes/admin/addThemeImg';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加图片成功！',
                type: 'success'
              });
              this.$router.push('SceneryListView');
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
    },
    loadThemeList(){
      let url = 'http://localhost:9580/themes/admin/themeName';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.themeOptions = responseBody.data;
      });
    },
    handleSuccess(response,file,fileList){
      this.ruleForm.url = response.data;
      console.log(this.ruleForm.url);
    },
    handleRemove(file, fileList) {
      //发出删除图片的请求
      let url = 'http://localhost:9580/remove?url=' + this.ruleForm.url;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .get(url).then(function () {

      })
      this.ruleForm.url = '';

    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  },
  mounted() {
    this.loadThemeList();
  }
}
</script>

<style scoped>

</style>